﻿using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZFSDomain.Common.CoreLib;
using ZFSDomain.Interface.User;
using ZFSDomain.Service;
using ZFSInterface.User;

namespace ZFSDomain.ViewModel.Sign
{

    /// <summary>
    /// 登录逻辑处理
    /// </summary>
    public class LoginViewModel : ViewModelBase
    {
        private string userName = string.Empty;
        private string passWord = string.Empty;

        /// <summary>
        /// 用户名
        /// </summary>
        public string UserName
        {
            get { return userName; }
            set { userName = value; RaisePropertyChanged(); }
        }

        /// <summary>
        /// 密码
        /// </summary>
        public string Password
        {
            get { return passWord; }
            set { passWord = value; RaisePropertyChanged(); }
        }

        public RelayCommand _signCommand;

        public RelayCommand SignCommand
        {
            get
            {
                if (_signCommand == null)
                {
                    _signCommand = new RelayCommand(() => Login());
                }
                return _signCommand;
            }
            set { _signCommand = value; RaisePropertyChanged(); }
        }

        /// <summary>
        /// 登陆系统
        /// </summary>
        public void Login()
        {
            if (!string.IsNullOrEmpty(UserName) && !string.IsNullOrEmpty(Password))
            {
                IUser user = ZFSBridge.BridgeFactory.GetUserBridge();
                var req = user.Login(UserName, Password);
                if (req != null)
                {
                    Messenger.Default.Send("", "Hide");

                    Loginer.LoginerUser.Account = req.Account;
                    Loginer.LoginerUser.UserName = req.UserName;
                    Loginer.LoginerUser.IsAdmin = req.FlagAdmin == "1" ? true : false;
                    Loginer.LoginerUser.Email = req.Email;

                    MainViewModel model = new MainViewModel();
                    model.PopBoxView = new Step.PopBoxViewModel();//设置自定义工具栏
                    model.LoadMenuByUserAuthority(req.Account);//加载菜单列表

                    var dialog = ServiceProvider.Instance.Get<IMainViewDialog>();
                    dialog.BindViewModel(model);
                    dialog.ShowDialog();
                }
            }
        }
    }
}
